/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { DecoratedV1VariableBase } from './decoratorBase';
import { IProvideDecoratedVariable, IVariableOwner, ConsumeOptions } from '../decorator';
import { WatchFuncType } from '../decorator';
import { IConsumeDecoratedVariable } from '../decorator';
import { ObserveSingleton } from '../base/observeSingleton';
import { NullableObject } from '../base/types';
import { UIUtils } from '../utils';
import { uiUtils } from '../base/uiUtilsImpl';
import { StateMgmtDFX } from '../tools/stateMgmtDFX';
import { ProvideDecoratedVariable } from './decoratorProvide';
export class ConsumeDecoratedVariable<T> extends DecoratedV1VariableBase<T> implements IConsumeDecoratedVariable<T> {
    provideAliasName: string;
    sourceProvide_: ProvideDecoratedVariable<T> | undefined;
    checkFake: boolean = false;
    constructor(owningView: IVariableOwner, varName: string, provideAliasName: string, watchFunc?: WatchFuncType, consumeOptions?: ConsumeOptions<T>) {
        super('@Consume', owningView, varName, watchFunc);
        this.provideAliasName = provideAliasName;
        this.sourceProvide_ = owningView.__findProvide__Internal<T>(provideAliasName) as ProvideDecoratedVariable<T> | undefined;
        if (!this.sourceProvide_) {
            if (consumeOptions) {
                this.checkFake = true;
                // use fake Provide source
                this.sourceProvide_ = new ProvideDecoratedVariable<T>(varName, uiUtils.makeV1Observed(consumeOptions!.defaultValue) as T);
                this.registerWatchForObservedObjectChanges(this.sourceProvide_!.get(false));
            } else {
                throw new Error(`Can not initialize @Consume. @Consume ${varName} has no default value or @Provide source!`);
            }
        } else {
            this.sourceProvide_!.registerWatchToSource(this);
        }
    }

    public get(): T {
        StateMgmtDFX.enableDebug && StateMgmtDFX.functionTrace(`Consume ${this.getTraceInfo()}`);
        return this.sourceProvide_!.get();
    }

    public set(newValue: T): void {
        const oldValue = this.sourceProvide_!.get(false);
        StateMgmtDFX.enableDebug && StateMgmtDFX.functionTrace(`Consume ${oldValue === newValue} ${this.setTraceInfo()}`);
        if (oldValue === newValue) {
            return;
        }
        if (!this.checkFake) {
            this.sourceProvide_!.set(newValue);
            return;
        }
        this.unregisterWatchFromObservedObjectChanges(oldValue);
        this.sourceProvide_!.set(newValue, false);
        this.registerWatchForObservedObjectChanges(this.sourceProvide_!.get(false));
        this.execWatchFuncs();
    }

    public getSource(): IProvideDecoratedVariable<T> {
        return this.sourceProvide_! as IProvideDecoratedVariable<T>;
    }
}
